<template>
  <div class="beds-list">
    <div class="beds-list-header">
      <div class="beds-list-from">
        <DynamicForm
          ref="dynamicFormRef"
          type="search"
          :rulesForm="state.searchData"
          :refresh="state.refresh"
          @click="callBack"
        />
      </div>
      <div class="beds-list-button">
        <el-button
          type="primary"
          @click="
            () => {
              toBedRouter();
            }
          "
          >新增</el-button
        >
      </div>
    </div>
    <div class="beds-list-table">
      <DynamicTable
        ref="dynamicTableRef"
        :columns="headerColumns"
        :table="state.table"
        :data="state.data"
        @click="callBack"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted, reactive } from "vue";
import DynamicForm from "com/layout/form/dynamic-form.vue";
import DynamicTable from "com/layout/table/dynamic-table.vue";
import { headerColumns } from "@/assets/script/middle/columns/supplier";
import { searchData } from "@/assets/script/middle/forms/supplier/search-form";
import { listCustomerPageApi } from "@/api/supplier";
import { listConstByDictionaryCodesApi } from "@/api/dictionary"; //数据字典
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
const dynamicFormRef = ref(DynamicForm);
const dynamicTableRef = ref(DynamicTable);
const router = useRouter();
const state = reactive({
  data: [],
  table: {
    total: 0,
    pageSize: 15,
    pageIndex: 1,
  },
  searchData: searchData as any,
  refresh: 0,
});
const natureCodeList = ref([]) as any; //公司性质
const customerTypeList = ref([]) as any; //公司类别
const card = state.searchData[0].card;
onMounted(() => {
  console.log(card);
  findPageListFun({ pageIndex: 1, pageSize: 15, deleted: 0 });
  listConstByDictionaryCodesApi(["customer_type", "nature_code"]).then(
    (data) => {
      data?.forEach((res: any) => {
        if (res.dictionaryCode == "nature_code") {
          natureCodeList.value.push({
            label: res.constName,
            value: res.constCode,
          });
        }
        if (res.dictionaryCode == "customer_type") {
          customerTypeList.value.push({
            label: res.constName,
            value: res.constCode,
          });
        }
      });
      card.find((f: any) => f.name === "natureCode").options =
        natureCodeList.value; //公司类别
      card.find((f: any) => f.name === "customerType").options =
        customerTypeList.value; //公司性质
    }
  );
});

const callBack = (data: any) => {
  switch (data.type || "") {
    //编辑
    case "edit":
      router.push({
        path: "/supplier/edit-supplier",
        query: { id: data.data.id },
      });
      break;
    //查询表单
    case "submit":
      console.log(data.data);
      findPageListFun({
        pageIndex: 1,
        pageSize: 15,
        deleted: 0,
        name: data.data.names,
        code: data.data.code,
        customerType: data.data.customerType,
        natureCode: data.data.natureCode,
      });
      break;
    //重置表单
    case "rest":
      state.searchData = searchData;
      state.table.pageIndex = 1;
      state.table.pageSize = 15;
      findPageListFun({
        pageIndex: 1,
        pageSize: 15,
        deleted: 0,
      });
      break;
    case "page":
      if (!state.searchData) {
        state.searchData = {};
      }
      state.searchData["pageIndex"] = data.data;
      state.searchData["pageSize"] = state.table.pageSize;
      state.table.pageIndex = data.data;
      findPageListFun({
        pageIndex: data.data,
        pageSize: state.table.pageSize,
        deleted: 0,
      });
      break;
    case "pageSize":
      if (!state.searchData) {
        state.searchData = {};
      }
      state.searchData["pageIndex"] = state.table.pageIndex;
      state.searchData["pageSize"] = data.data;
      state.table.pageSize = data.data;
      findPageListFun({
        pageIndex: data.data,
        pageSize: state.table.pageSize,
        deleted: 0,
      });
      break;
  }
};
//获取列表数据方法
const findPageListFun = (params: any) => {
  console.log(params);
  listCustomerPageApi(params).then((data) => {
    if (data.code == 0) {
      if (data.data.length != 0) {
        state.data = data.data;
      } else {
        state.data = [];
      }
      state.table.total = data.total;
      state.table.pageIndex = data.pageIndex;
    }
  });
};
//新增
const toBedRouter = () => {
  router.push({ path: "/supplier/add-supplier" });
};
</script>
